// 生成 computens
/**
* arguments : [key], commit, modulesPath, attr
* arguments : [{key: 'name', commit: 'commit', modulesPath: 'a/b', attr}]
**/


const createMap = (params) => {
  const res = {};
  params.forEach(item => {
    let k = item['key'],
        attr = item['attr'];
    res[k] = {
      get () {
        let paths = item.modelPath ? item.modelPath.split('/') : [];
        if (attr) {
          paths.push(attr);
        }
        let result = paths.reduce(function (store, path) {
            return store[path];
          }, this.$store.state)
        return result[k]
      },
      set (val) {
        let opts = {}, mutation;
        opts[k] = val;
        mutation = item.modelPath ? `${item.modelPath}/${item.commit}` : item.commit;
        this.$store.commit(mutation, opts);
      }
    }
  })
  return res
}


export default (...args) => {
  let list = [];
  if (args.length > 1) {
    let keys = args.shift();
    keys.forEach( key => {
      list.push({
        "key": key,
        "commit": args[0],
        "modelPath": args[1],
        "attr": args[2] || ''
      })
    })
  } else {
    list = args[0]
  }
  return createMap(list)
}
